﻿using Microsoft.AspNetCore.Mvc;
using ProjectScheduling_web.dao;
using ProjectScheduling_web.model;
using ProjectScheduling_web.model.Enums;

namespace ProjectScheduling_web.Controllers.U
{
    public class CustomerController : BController
    {
        private static readonly CustomerDao @base = CustomerDao.Base;
        [HttpGet]
        public IActionResult Search(int page, int row, string key, EnumCustomerType type, string salerName, string start, string end)
        {
            var _start = DateTime.TryParse(start, out DateTime startTime) ? startTime : DateTime.MinValue;
            var _end = DateTime.TryParse(end, out DateTime endTime) ? endTime.AddDays(1) : DateTime.MaxValue;
            var lst = @base.FindAll(page, row, it =>
            (string.IsNullOrEmpty(key) || (it.Name != null && it.Name.Contains(key))) &&
            (type == 0 || it.Type == type) &&
            (string.IsNullOrEmpty(salerName) || (it.SalerName != null && it.SalerName.Contains(salerName))) &&
            (string.IsNullOrEmpty(start) || (it.CreateTime > _start)) &&
            (string.IsNullOrEmpty(end) || (it.CreateTime < _end))
            );
            return Success(lst);
        }
        [HttpPost]
        public IActionResult Add([FromBody] Customer req)
        {
            if (HttpContext.Items["Current_Msg"] is not Member mem) return Fail("获取登录状态有误");
            if (req == null) return Fail("数据有误");
            if (req.SalerId != 0)
            {
                mem = MemberDao.Base.Find(req.SalerId);
            }
            if (string.IsNullOrEmpty(req.Name)) return Fail("客户名称不能为空");
            if (@base.Exists(it => it.Name == req.Name && it.SalerId == mem.Id))
                return Fail("当前客户已存在，请勿重复添加");
            req.CreateTime = DateTime.Now;
            if (req.SalerId == 0)
            {
                req.SalerId = mem.Id;
                req.SalerName = mem.Name;
            }
            else
            {
                var saler = MemberDao.Base.Find(req.SalerId);
                if (saler == null) return Fail("当前销售人员不存在");
                req.SalerName = saler.Name;
            }
            @base.Add(req);
            return Success("创建成功");
        }
        [HttpPost]
        public IActionResult Update([FromBody] Customer req)
        {
            if (req == null) return Fail("数据有误");
            if (string.IsNullOrEmpty(req.Name)) return Fail("客户名称不能为空");
            if (req.Id == 0) return Fail("获取客户信息有误");
            var obj = @base.Find(req.Id);
            if (obj == null) return Fail("当前客户不存在");
            obj.Name = req.Name;
            obj.Phone = req.Phone;
            obj.Address = req.Address;
            obj.Linker = req.Linker;
            obj.SalerId = req.SalerId;
            obj.SalerName = req.SalerName;
            @base.Update(obj, it => new { it.Name, it.Phone, it.Address, it.Linker, it.SalerId, it.SalerName });
            return Success("修改成功");
        }
        [HttpGet]
        public IActionResult Remove(int id)
        {
            if (id == 0) return Fail("获取客户信息有误");
            var obj = @base.Find(id);
            if (obj == null) return Fail("当前客户不存在");
            @base.Delete(id);
            return Success("删除成功");
        }
        [HttpGet]
        public IActionResult SearchName(string key)
        {
            var lst = @base.SearchNameByKey(key);
            return Success(lst);
        }
    }
}
